package cn.itsource.pethome.basic.service.impl;

import cn.itsource.pethome.basic.mapper.BaseMapper;
import cn.itsource.pethome.basic.service.BaseService;


import cn.itsource.pethome.basic.query.BaesQuery;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);

    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T loadById(Long id) {
        Object t = baseMapper.loadById(id);
        return (T)t;
    }

    @Override
    public List<T> loadAll() {
        List<T> ts = baseMapper.loadAll();
        return ts;
    }


    @Override
    public PageList<T> loadPageByQuery(BaesQuery query) {
        //1.查询总条数如果为0就不用分页了
        System.out.println(query);
        Long total = baseMapper.findCountByQuery(query);
        if (total.equals(0L)){
            return new PageList<T>();
        }
        //查询分页列表
        List<T> result = baseMapper.fandListByQuery(query);
            return new PageList<T>(total,result);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

}
